package com.ruoyi.worker.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.worker.domain.TimeShift;
import com.ruoyi.worker.mapper.TimeShiftMapper;
import com.ruoyi.worker.service.TimeShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TimeShiftServiceImpl
 * @Description
 * @Author leizhang
 * Date 2021/8/29 1:55 下午
 * @Version 1.0
 **/
@Service
public class TimeShiftServiceImpl implements TimeShiftService {
     @Autowired
     private TimeShiftMapper timeShiftMapper;
     @Autowired
     private RedisCache redisCache;
    @Override
    public List<TimeShift> getTimeShiftList(TimeShift timeShift) {
        List<TimeShift> timeShiftList = timeShiftMapper.getTimeShiftList(timeShift);
        Map<String, Object> workerNumber = redisCache.getCacheMap(String.valueOf(timeShift.getCompanyId()));
        workerNumber.forEach((key,value) -> {
            for (TimeShift shift : timeShiftList) {
                int type = shift.getType();
                shift.setMaleNumber(workerNumber.get(String.valueOf(type).concat("_").concat("1"))==null?0:(int)workerNumber.get(String.valueOf(type).concat("_").concat("1")));
                shift.setFelNumber(workerNumber.get(String.valueOf(type).concat("_").concat("2"))==null?0:(int)workerNumber.get(String.valueOf(type).concat("_").concat("2")));
            }
        });
        return timeShiftList;
    }

    @Override
    public int updateStatus(Map<String, Object> requestParam) {
        return timeShiftMapper.updateStatus(requestParam);
    }

    @Override
    public int insertTimeShift(TimeShift timeShift) {
        return timeShiftMapper.insertTimeShift(timeShift);
    }

    @Override
    public TimeShift getTimeShift(int id) {
        return timeShiftMapper.getTimeShift(id);
    }

    @Override
    public int deleteTimeShift(int id) {
        return timeShiftMapper.delTimeShiftById(id);
    }

    @Override
    public int updateTimeShift(TimeShift timeShift) {
        return timeShiftMapper.updateTimeShift(timeShift);
    }

    @Override
    public int saveNumber(Map<String, Object> requestParam) throws Exception{
        //获取companyId
        String companyId = requestParam.get("companyId").toString();
        //性别
        String sex = requestParam.get("sex").toString();
        redisCache.hset(companyId,String.valueOf(requestParam.get("type")).concat("_").concat(sex),new Integer(requestParam.get("number").toString()), DateUtils.getOverTime());
        return 1;
    }
}
